package com.zeusee.main.lpr.jni;

import android.util.Log;


import com.zeusee.main.lpr.bean.PlateInfo;

import java.util.ArrayList;
import java.util.List;

public class PlateRecognition {
    public List<PlateInfo> ques;

    private static native void InitPlateRecognizer(String str);

    private static native String RunRecognitionAsRaw(byte[] bArr, int i, int i2, float f, int i3, int i4, int i5);

    private static native String RunRecognitionByPath(String str, float f, int i, int i2, int i3);

    private static native void releasePlateRecognizer();

    static {
        System.loadLibrary("hyperlpr");
    }

    private static class PlateRecognizerHolder {
        /* access modifiers changed from: private */
        public static final PlateRecognition INSTANCE = new PlateRecognition();

        private PlateRecognizerHolder() {
        }
    }

    public static final PlateRecognition getInstance() {
        return PlateRecognizerHolder.INSTANCE;
    }

    private PlateRecognition() {
    }

    public void initPlateRecognition(String str) {
        InitPlateRecognizer(str);
        this.ques = new ArrayList();
    }

    private static List<PlateInfo> parserPlate(String str) {
        ArrayList arrayList = new ArrayList();
        if (str.indexOf("sessionError") != -1) {
            arrayList.add(new PlateInfo(Integer.parseInt(str.split("/")[1])));
        }
        String[] split = str.split("/");
        int i = 0;
        while (i < split.length) {
            String[] split2 = split[i].split(",");
            if (split2.length == 1) {
                break;
            } else if (split2.length == 7) {
                arrayList.add(new PlateInfo(split2[0].trim(), Integer.parseInt(split2[1]), Integer.parseInt(split2[2]), Integer.parseInt(split2[3]), Integer.parseInt(split2[4]), Integer.parseInt(split2[5]), Float.parseFloat(split2[6])));
                i++;
            } else {
                throw new ArrayIndexOutOfBoundsException("");
            }
        }
        return arrayList;
    }

    private PlateInfo getMaxPlateInfo(List<PlateInfo> list) {
        PlateInfo plateInfo = list.get(0);
        for (int i = 1; i < list.size(); i++) {
            if (list.get(i).getConfidence() > plateInfo.getConfidence()) {
                plateInfo = list.get(i);
            }
        }
        return plateInfo;
    }

    public List<PlateInfo> plateRecognitionByPath(String str, int i, int i2, int i3) {
        String RunRecognitionByPath = RunRecognitionByPath(str, 0.8f, i, i2, i3);
        Log.d("concat_res", RunRecognitionByPath);
        List<PlateInfo> parserPlate = parserPlate(RunRecognitionByPath);
        if (parserPlate.size() == 0) {
            this.ques.clear();
        } else {
            PlateInfo maxPlateInfo = getMaxPlateInfo(parserPlate);
            if (this.ques.size() < 2) {
                this.ques.add(maxPlateInfo);
            } else {
                this.ques.remove(0);
                this.ques.add(maxPlateInfo);
            }
        }
        return parserPlate;
    }

    public List<PlateInfo> plateRecognitionWithRaw(byte[] bArr, int i, int i2, float f, int i3, int i4, int i5) {
        String RunRecognitionAsRaw = RunRecognitionAsRaw(bArr, i, i2, 0.8f, i3, i4, i5);
        Log.d("concat_res", RunRecognitionAsRaw);
        List<PlateInfo> parserPlate = parserPlate(RunRecognitionAsRaw);
        for (PlateInfo info : parserPlate){
            Log.d("PlateName", info.getPlateName());
        }
        if (parserPlate.size() == 0) {
            this.ques.clear();
        } else {
            PlateInfo maxPlateInfo = getMaxPlateInfo(parserPlate);
            if (this.ques.size() < 2) {
                this.ques.add(maxPlateInfo);
            } else {
                this.ques.remove(0);
                this.ques.add(maxPlateInfo);
            }
        }
        return parserPlate;
    }

    public PlateInfo getBestConfidenceInfo() {
        Log.d("debug_d", String.valueOf(this.ques.size()));
        if (this.ques.size() >= 2) {
            return getMaxPlateInfo(this.ques);
        }
        return new PlateInfo(0);
    }

    public void releaseJni() {
        releasePlateRecognizer();
    }
}
